package vikaa.tools;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author wheal
 *	�����������������
 */
public class CalendarUtil{
	private int days;// ����ȫ�ֿ��� ǰһ�죬���죬��һ�������仯
	private int weeks;// ����ȫ�ֿ��� ��һ�ܣ����ܣ���һ�ܵ�����仯
	private int months;// ����ȫ�ֿ��� ��һ�£����£���һ�µ�����仯
	private int FirstDayOfWeek = 0;//0������������տ�ʼ��1�������һ��ʼ
	
	public CalendarUtil(int data) {
		super();
		this.days = data;
		this.weeks = data;
		this.months = data;
	}
	
	public CalendarUtil() {
		super();
	}



	/**
	 * ��ȡ��������
	 * @param dateformat
	 * @return
	 */
	public Date getNowTime() {
		Date now = new Date();
		return now;
	}
	/**
	 * ���һ�����ڣ����������ڼ����ַ�
	 * @param sdate
	 * @return
	 */
	public String getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return new SimpleDateFormat("E").format(c.getTime());
	}
	/**
	 * ��ȡ�Ե���Ϊ��ǰ���������������
	 * @return
	 */
	public Date getDay(){
		Calendar date = Calendar.getInstance();
		date.add(Calendar.DATE, days);
		return date.getTime();
	}
	/**
	 * ��ȡ��ǰ�����뱾�ܿ�ʼ��������
	 * @return 
	 */
	public int getDayPlus(){
		Calendar c = Calendar.getInstance();
		int daysOfWeek = c.get(Calendar.DAY_OF_WEEK)-FirstDayOfWeek;
		if (daysOfWeek == 1) 
			return 0;
		else 
			return 1 - daysOfWeek;
	}
	/**
	 * ��ȡĳһ�ܵĿ�ʼ����
	 * @return 
	 */
	public Date getFirstDayofWeek(){
		int dayPlus = this.getDayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, dayPlus + (weeks * 7));
		return currentDate.getTime();
	}
	/**
	 * ��ȡĳһ�ܵĽ�������
	 * @return
	 */
	public Date getLastDayofWeek(){
		int dayPlus = this.getDayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, dayPlus + 6 + (weeks * 7));
		return currentDate.getTime();
	}
	/**
	 * ��ȡĳһ�µĿ�ʼ����
	 * @return
	 */
	public Date getFirstDayofMonth() {
		Calendar firstDate = Calendar.getInstance();
		firstDate.set(Calendar.DATE, 1);// ��Ϊ��ǰ�µ�1��
		firstDate.add(Calendar.MONTH, months);
		return firstDate.getTime();
	}

	/**
	 * ��ȡĳһ�µĽ�������
	 * @return
	 */
	public Date getLastDayofMonth(){
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// ��Ϊ��ǰ�µ�1��
		lastDate.add(Calendar.MONTH, months+1);
		lastDate.add(Calendar.DATE, -1);
		return lastDate.getTime();
	}
	/**
	 * ��ĳһ��ĵڼ��ܻ�ȡ��ʼ���������
	 * @param week
	 * @param year
	 * @param dateformat
	 * @return
	 */
	public String getFirstdateNLastdateOfWeek(int week,int year,String dateformat){
		Calendar calFirstDayOfTheYear = new GregorianCalendar(year,Calendar.JANUARY, 1);
    	//����week�ܵ�����õ����ܵĵ�һ������
    	calFirstDayOfTheYear.add(Calendar.DATE, 7 * (week-1));
    	//��ø����������ڼ�
    	int dayOfWeek = calFirstDayOfTheYear.get(Calendar.DAY_OF_WEEK); 
    	Calendar calFirstDayInWeek = (Calendar)calFirstDayOfTheYear.clone();
    	//��ȡ�������ڵĸ��ܵĵ�һ��
    	calFirstDayInWeek.add(Calendar.DATE,calFirstDayOfTheYear.getActualMinimum(Calendar.DAY_OF_WEEK) - dayOfWeek);
    	Date firstDayInWeek = calFirstDayInWeek.getTime();
    	Calendar calLastDayInWeek = (Calendar)calFirstDayOfTheYear.clone();
    	//��ȡ�������ڵĸ��ܵ����һ��
    	calLastDayInWeek.add(Calendar.DATE, calFirstDayOfTheYear.getActualMaximum(Calendar.DAY_OF_WEEK) - dayOfWeek);
    	Date lastDayInWeek = calLastDayInWeek.getTime();
    	String str = Date2String(firstDayInWeek,dateformat)+"-"+Date2String(lastDayInWeek,dateformat);
    	return str;
	}
	/**
	 * ��Dateת��ΪString
	 * @param date ,
	 * @return
	 */
	public String Date2String(Date date){
		SimpleDateFormat df = new SimpleDateFormat();
		return df.format(date);
	}
	
	/**
	 * ��Date����dateformat��ʽת��ΪString
	 * @param date
	 * @param dateformat
	 * @return
	 */
	public static String Date2String(Date date,String dateformat){
		SimpleDateFormat df = new SimpleDateFormat(dateformat);
		return df.format(date);
	}
	
	/**
	 * ��stringת��date
	 * @param strdate
	 * @return
	 */
	public Date String2Date(String strdate){
		SimpleDateFormat formatter = new SimpleDateFormat();  
        ParsePosition pos = new ParsePosition(0);  
        return formatter.parse(strdate, pos);
	}
	/**
	 * ��stringת����dateformat��ʽ��date
	 * @param strdate 
	 * @param dateformat
	 * @return
	 */
	public Date String2Date(String strdate,String dateformat){
		SimpleDateFormat formatter = new SimpleDateFormat(dateformat);  
        ParsePosition pos = new ParsePosition(0);  
        return formatter.parse(strdate, pos);
	}
}
